package com.zw.yzk.kabu.wxapi;


import android.content.Context;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.management.ToastManager;

/**
 * Created by zhanwei on 2017/2/28.
 */

public class WechatManager {
    //微信开发者ID
    public static final String WECHAT_APP_ID = "wx2ab8f4f8ca0300f1";
    private static IWXAPI api;

    public static void payByWeChat(Context context, String appId, String partnerId, String prepayId, String packageName,
                                   String nonceStr, String timeStamp, String sign) {
        if (api == null) {
            api = WXAPIFactory.createWXAPI(context, WechatManager.WECHAT_APP_ID);
        }

        PayReq payReq = new PayReq();
        payReq.appId = appId;
        payReq.partnerId = partnerId;
        payReq.prepayId = prepayId;
        payReq.packageValue = packageName;
        payReq.nonceStr = nonceStr;
        payReq.timeStamp = timeStamp;
        payReq.sign = sign;

        api.sendReq(payReq);
    }

    public static void loginbyWeichat(Context context) {
        if (api == null) {
            api = WXAPIFactory.createWXAPI(context, WechatManager.WECHAT_APP_ID);
        }

        if (!api.isWXAppInstalled())
        {
            //提醒用户没有安装微信
            ToastManager.getInstance().showToast(context, context.getResources().getString(R.string.please_install_wechat_first));
            return;
        }

        api.registerApp(WECHAT_APP_ID);

        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "kabu-Android";

        api.sendReq(req);
    }
}
